const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'chatime',
  port: 3306
};

async function checkAvatars() {
  const pool = mysql.createPool(dbConfig);
  
  try {
    console.log('🔗 连接数据库...');
    await pool.getConnection();
    console.log('✅ 数据库连接成功');
    
    // 查询所有用户的头像信息
    console.log('📋 查询用户头像信息...');
    const [users] = await pool.execute(`
      SELECT id, name, avatar, create_time FROM users ORDER BY create_time
    `);
    
    console.log(`📊 找到 ${users.length} 个用户`);
    console.log('\n👥 用户头像分配情况：');
    console.log('─'.repeat(60));
    console.log('ID\t\t\t\t\t\t姓名\t\t头像\t\t创建时间');
    console.log('─'.repeat(60));
    
    users.forEach(user => {
      const avatar = user.avatar || '无头像';
      const createTime = new Date(user.create_time).toLocaleString('zh-CN');
      console.log(`${user.id}\t${user.name}\t\t${avatar}\t\t${createTime}`);
    });
    
    console.log('─'.repeat(60));
    
    // 统计头像使用情况
    const avatarStats = {};
    users.forEach(user => {
      const avatar = user.avatar || '无头像';
      avatarStats[avatar] = (avatarStats[avatar] || 0) + 1;
    });
    
    console.log('\n📈 头像使用统计：');
    Object.entries(avatarStats).forEach(([avatar, count]) => {
      console.log(`  ${avatar}: ${count} 个用户`);
    });
    
  } catch (error) {
    console.error('❌ 查询头像信息失败:', error);
  } finally {
    await pool.end();
  }
}

// 运行脚本
checkAvatars(); 